﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAOTicketReservationSystem;

namespace ManageEvents
{
    public partial class FormAddEditEvents : Form
    {
        private Dao dao;
        private int eventId;
        private bool isForDetails;
        private bool isForEdit;
        private bool isForAdd;

        public bool IsForAdd
        {
            set { isForAdd = value; }
        }

        public bool IsForEdit
        {
            set { isForEdit = value; }
        }

        public bool IsForDetails
        {
            set { isForDetails = value; }
        }

        public int EventId
        {
            set { eventId = value; }
        }

        public FormAddEditEvents()
        {
            InitializeComponent();
        }

        private void FormAddEditEvents_Load(object sender, EventArgs e)
        {
            dao = new Dao();

            if (this.isForDetails)
            {
                textBoxEventName.Enabled = false;
                textBoxEventPrice.Enabled = false;
                comboBoxEventCategory.Enabled = false;
                comboBoxEventLocation.Enabled = false;
                dateTimePickerEventDate.Enabled = false;
                this.Text = "Event details";
                BindEventWithControlsForDetails();
            }

            if (this.isForAdd)
            {
                buttonAdd.Visible = true;
                buttonClear.Visible = true;
                BindComboBoxes();
            }

            if (this.isForEdit)
            {
                buttonSave.Visible = true;
                buttonCancel.Visible = true;
                BindEventWithControlsForEdit();
            }
        }

        private void BindComboBoxes()
        {
            comboBoxEventCategory.DataSource = dao.GetAllCategories();
            comboBoxEventCategory.ValueMember = "CategoryID";
            comboBoxEventCategory.DisplayMember = "CategoryName";
            comboBoxEventLocation.DataSource = dao.GetAllLocations();
            comboBoxEventLocation.ValueMember = "LocationID";
            comboBoxEventLocation.DisplayMember = "LocationName";
        }

        private void BindEventWithControlsForDetails()
        {
            Event currentEvent = dao.GetEventById(this.eventId);
            textBoxEventName.Text = currentEvent.EventName;
            textBoxEventPrice.Text = currentEvent.Price.ToString();
            comboBoxEventCategory.Items.Add(currentEvent.Category.CategoryName);
            comboBoxEventCategory.Text = comboBoxEventCategory.Items[0].ToString();
            comboBoxEventLocation.Items.Add( currentEvent.Location.LocationName);
            comboBoxEventLocation.Text = comboBoxEventLocation.Items[0].ToString();
            dateTimePickerEventDate.Value = currentEvent.Date;
        }

        private void BindEventWithControlsForEdit()
        {
            Event currentEvent = dao.GetEventById(this.eventId);
            textBoxEventName.Text = currentEvent.EventName;
            textBoxEventPrice.Text = currentEvent.Price.ToString();

            comboBoxEventCategory.DataSource = dao.GetAllCategories();
            comboBoxEventCategory.ValueMember = "CategoryID";
            comboBoxEventCategory.DisplayMember = "CategoryName";
            comboBoxEventCategory.SelectedValue = currentEvent.CategoryID;

            comboBoxEventLocation.DataSource = dao.GetAllLocations();
            comboBoxEventLocation.ValueMember = "LocationID";
            comboBoxEventLocation.DisplayMember = "LocationName";
            comboBoxEventLocation.SelectedValue = currentEvent.LocationID;
            dateTimePickerEventDate.Value = currentEvent.Date;
        }

        private bool CheckPrice()
        {
            decimal price = decimal.MinValue;
            if (decimal.TryParse(textBoxEventPrice.Text, out price))
            {
                if (price > 0)
                {
                    return true;
                }
            }
            return false;
        }

        private bool CheckName()
        {
            if (textBoxEventName.Text.Trim().Length > 0)
            {
                return true;
            }
            return false;
        }

        protected void AddEvent()
        {
            Event newEvent = new Event();
            newEvent.CategoryID = (int)comboBoxEventCategory.SelectedValue;
            newEvent.LocationID = (int)comboBoxEventLocation.SelectedValue;
            newEvent.EventName = textBoxEventName.Text.Trim();
            newEvent.Date = dateTimePickerEventDate.Value;
            newEvent.Price = decimal.Parse(textBoxEventPrice.Text);
            string addEventResult = dao.AddEvent(newEvent);
            if (addEventResult == Dao.GetSuccessMessage())
            {
                MessageBox.Show("Event added", "Success");
            }
            else
            {
                MessageBox.Show(addEventResult, "Error");
            }
        }

        private void buttonBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            if (CheckPrice())
            {
                if (CheckName())
                {
                    AddEvent();
                }
                else
                {
                    MessageBox.Show("Enter event name!", "Error");
                }
            }
            else
            {
                MessageBox.Show("Enter valid price", "Error");
            } 
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            FormAddEditEvents_Load(null, EventArgs.Empty);
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            textBoxEventName.Text = "";
            textBoxEventPrice.Text = "";
            dateTimePickerEventDate.Value = DateTime.Now;
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (CheckPrice())
            {
                if (CheckName())
                {
                    EditEvent();
                }
                else
                {
                    MessageBox.Show("Enter event name!", "Error");
                }
            }
            else
            {
                MessageBox.Show("Enter valid price", "Error");
            } 
        }

        private void EditEvent()
        {
            Event newEvent = new Event();
            newEvent.CategoryID = (int)comboBoxEventCategory.SelectedValue;
            newEvent.LocationID = (int)comboBoxEventLocation.SelectedValue;
            newEvent.EventName = textBoxEventName.Text.Trim();
            newEvent.Date = dateTimePickerEventDate.Value;
            newEvent.Price = decimal.Parse(textBoxEventPrice.Text);
            newEvent.EventID = eventId;
            string editEventResult = dao.EditEvent(newEvent);
            if (editEventResult == Dao.GetSuccessMessage())
            {
                MessageBox.Show("Event edited", "Success");
            }
            else
            {
                MessageBox.Show(editEventResult, "Error");
            }
        }
    }
}
